package com.youlai.boot.system.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.SysDianciMaterialRetrospectMapper;
import com.youlai.boot.system.service.SysDianciMaterialRetrospectService;
import com.youlai.boot.system.model.entity.SysDianciMaterialRetrospect;
import com.youlai.boot.system.model.form.SysDianciMaterialRetrospectForm;
import com.youlai.boot.system.model.query.SysDianciMaterialRetrospectQuery;
import com.youlai.boot.system.model.vo.SysDianciMaterialRetrospectVO;
import com.youlai.boot.system.converter.SysDianciMaterialRetrospectConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 原料追溯服务实现类
 *
 * @author liwenbing
 * @since 2025-04-09 13:23
 */
@Service
@RequiredArgsConstructor
public class SysDianciMaterialRetrospectServiceImpl extends ServiceImpl<SysDianciMaterialRetrospectMapper, SysDianciMaterialRetrospect> implements SysDianciMaterialRetrospectService {

    private final SysDianciMaterialRetrospectConverter sysDianciMaterialRetrospectConverter;

    /**
    * 获取原料追溯分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<SysDianciMaterialRetrospectVO>} 原料追溯分页列表
    */
    @Override
    public IPage<SysDianciMaterialRetrospectVO> getSysDianciMaterialRetrospectPage(SysDianciMaterialRetrospectQuery queryParams) {
        Page<SysDianciMaterialRetrospectVO> pageVO = this.baseMapper.getSysDianciMaterialRetrospectPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取原料追溯表单数据
     *
     * @param id 原料追溯ID
     * @return
     */
    @Override
    public SysDianciMaterialRetrospectForm getSysDianciMaterialRetrospectFormData(Long id) {
        SysDianciMaterialRetrospect entity = this.getById(id);
        return sysDianciMaterialRetrospectConverter.toForm(entity);
    }
    
    /**
     * 新增原料追溯
     *
     * @param formData 原料追溯表单对象
     * @return
     */
    @Override
    public boolean saveSysDianciMaterialRetrospect(SysDianciMaterialRetrospectForm formData) {
        SysDianciMaterialRetrospect entity = sysDianciMaterialRetrospectConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新原料追溯
     *
     * @param id   原料追溯ID
     * @param formData 原料追溯表单对象
     * @return
     */
    @Override
    public boolean updateSysDianciMaterialRetrospect(Long id,SysDianciMaterialRetrospectForm formData) {
        SysDianciMaterialRetrospect entity = sysDianciMaterialRetrospectConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除原料追溯
     *
     * @param ids 原料追溯ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteSysDianciMaterialRetrospects(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的原料追溯数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
